/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Book;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

/**
 *
 * @author daz
 */
public class BookDao extends SimpleJdbcDaoSupport {

    public List<Book> searchBook(String key, String option) {
        String query = "";
        if (option.equals("0")) {
            query = "SELECT * FROM bklib.book b where publisher like '%" + key + "%'";
        } else if (option.equals("1")) {
            query = "SELECT * FROM bklib.book b where author like '%" + key + "%'";
        } else if (option.equals("2")) {
            query = "SELECT * FROM bklib.book b where title like '%" + key + "%'";
        } else {
            query = "SELECT * FROM bklib.book b where id =" + key;
        }
        return getSimpleJdbcTemplate().query(query, new BookMapper());
    }

    private static class BookMapper implements ParameterizedRowMapper<Book> {

        public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
            Book book = new Book();
            book.setAuthor(rs.getString("author"));
            book.setPublisher(rs.getString("publisher"));
            book.setTitle(rs.getString("title"));
            book.setYear(rs.getString("year"));
            book.setCode(rs.getString("code"));
            book.setId(rs.getInt("id"));
            return book;
        }
    }
}
